package com.deeper.customview.ui.c9.demo.c09d03;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 用于滑出删除的列表项。
 */
public class ExtendLayout extends ViewGroup {
    public ExtendLayout(Context context) {
        this(context, null);
    }


    public ExtendLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }


    public ExtendLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TextView tvDelete = createDeleteButton();
        this.addView(tvDelete, 0);
    }


    @Override
    public void addView(View child, int width, int height) {
        super.addView(child, width, height);
        if (getChildCount() > 2) {
            throw new IndexOutOfBoundsException("Sub views is too many;");
        }
    }


    private TextView createDeleteButton() {
        TextView tvDelete = new TextView(getContext());
        tvDelete.setText("删除");
        tvDelete.setGravity(Gravity.CENTER);
        tvDelete.setBackgroundColor(Color.RED);
        tvDelete.setTextColor(Color.WHITE);

        final float scale = getContext().getResources().getDisplayMetrics().density;
        int px = (int) (10 * scale + 0.5);
        tvDelete.setPadding(px * 2, px, px * 2, px);

        LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.MATCH_PARENT);
        tvDelete.setLayoutParams(lp);

        tvDelete.setClickable(true);

        return tvDelete;
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        measureChildren(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec));
    }


    /**
     * 测量宽度，额外添加删除按钮的宽度
     */
    private int measureWidth(int widthMeasureSpec) {
        int mode = MeasureSpec.getMode(widthMeasureSpec);
        int size = MeasureSpec.getSize(widthMeasureSpec);
        int width = 0;
        int tvDeleteWidth = 0;

        if (mode == MeasureSpec.EXACTLY) {
            width = size;
        } else if (mode == MeasureSpec.AT_MOST || mode == MeasureSpec.UNSPECIFIED) {
            for (int i = 0; i < getChildCount(); i++) {
                if (i == 1) {
                    width = getChildAt(i).getMeasuredWidth();
                }
            }
        }

        for (int i = 0; i < getChildCount(); i++) {
            if (i == 0) {
                tvDeleteWidth = getChildAt(i).getMeasuredWidth();
            }
        }

        return width + tvDeleteWidth;
    }


    /**
     * 测量高度.高度以LinearLayout 子容器为准，删除按钮必须与LinearLayout保持一样的高度
     */
    private int measureHeight(int heightMeasureSpec) {
        int mode = MeasureSpec.getMode(heightMeasureSpec);
        int size = MeasureSpec.getSize(heightMeasureSpec);
        int height = 0;

        if (mode == MeasureSpec.EXACTLY) {
            height = size;
        } else if (mode == MeasureSpec.UNSPECIFIED || mode == MeasureSpec.AT_MOST) {
            for (int i = 0; i < getChildCount(); i++) {
                if (i == 1) {
                    height = getChildAt(1).getMeasuredHeight();
                }
            }
        }
        //重新调整删除按钮高度
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, height);
        for (int i = 0; i < getChildCount(); i++) {
            if (i == 0) {
                getChildAt(0).setLayoutParams(layoutParams);
            }
        }

        return height;
    }


    /**
     * 默认情况下，删除按钮不出现，这里加以隐藏
     */
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        View tvDelete = getChildAt(0);
        int tvDeleteWidth = tvDelete.getMeasuredWidth();
        int tvDeleteHeight = tvDelete.getMeasuredHeight();
        tvDelete.layout(0, 0, tvDeleteWidth, tvDeleteHeight);
        getChildAt(1).layout(tvDeleteWidth, 0, getMeasuredWidth(), getMeasuredHeight());
        //隐藏删除按钮
        this.scrollTo(tvDeleteWidth, 0);
    }


    public View getDeleteButton() {
        return getChildAt(0);
    }
}
